﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
namespace DAO
{
    public class TinBanDAO
    {
        #region Phần của Luân
        public static TinBanDTO LayTinBanTuDataRow(DataRow dr)
        {
            TinBanDTO tb = null;
            try
            {
                tb = new TinBanDTO();
                tb.MaTinBan = dr["MaTinBan"].ToString();
                tb.MaSanPham = dr["MaSanPham"].ToString();
                tb.MaNguoiBan = dr["MaNguoiBan"].ToString();
                tb.TieuDe = dr["TieuDe"].ToString();
                tb.AnhDaiDien = dr["AnhDaiDien"].ToString();
                tb.ThoiGianBan = DateTime.Parse(dr["ThoiGianBan"].ToString());
                tb.GiaBan = int.Parse(dr["GiaBan"].ToString());
                tb.TinhTrangTinBan = dr["TinhTrangTinBan"].ToString();
                tb.HinhThucBan = dr["HinhThucBan"].ToString();
                tb.LoaiVanChuyen = dr["LoaiVanChuyen"].ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy thông tin tin bán: " + ex.ToString());
            }
            return tb;
        }

        public static IList<SqlParameter> TruyenThamSoChoStored(TinBanDTO item)
        {
            IList<SqlParameter> arrParameter = new List<SqlParameter>();
            arrParameter.Add(new SqlParameter("@MaTinBan", item.MaTinBan));
            arrParameter.Add(new SqlParameter("@MaSanPham", item.MaSanPham));
            arrParameter.Add(new SqlParameter("@MaNguoiBan", item.MaNguoiBan));
            arrParameter.Add(new SqlParameter("@TieuDe", item.TieuDe));
            arrParameter.Add(new SqlParameter("@AnhDaiDien", item.AnhDaiDien));
            arrParameter.Add(new SqlParameter("@ThoiGianBan", item.ThoiGianBan));
            arrParameter.Add(new SqlParameter("@GiaBan", item.GiaBan));
            arrParameter.Add(new SqlParameter("@TinhTrangTinBan", item.TinhTrangTinBan));
            arrParameter.Add(new SqlParameter("@HinhThucBan", item.HinhThucBan));
            arrParameter.Add(new SqlParameter("@LoaiVanChuyen", item.LoaiVanChuyen));
            return arrParameter;
        }

        public static DataTable TimTinBanTheoTenSanPham(string tenSanPham, string loaiSanPham)
        {
            DataTable dt = null;
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@TenSanPham", tenSanPham));
                arrParameter.Add(new SqlParameter("@LoaiSanPham", loaiSanPham));
                dt = DataProvider.ExecuteStoreProc("TimKiemSanPhamTheoTen", arrParameter);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi tbi thêm mới tbách hàng " + ex.ToString());
            }
            return dt;

        }
            
        public static DataTable HienThiChiTietTinBan(string maTinBan)
        {
            DataTable dt = null;
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@MaTinBan", maTinBan));
                dt = DataProvider.ExecuteStoreProc("HienThiChiTietTinBan", arrParameter);
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi tbi thêm mới tbách hàng " + ex.ToString());
            }
            return dt;

        }

        //thêm tin bán
        public static void ThemTinBan(ref TinBanDTO tb)
        {
            try
            {
                IList<SqlParameter> arrParameter = TruyenThamSoChoStored(tb);
                DataTable dt = DataProvider.ExecuteStoreProc("proc_InsertTheDocGia", arrParameter);
                tb.MaTinBan = dt.Rows[0][0].ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thêm mới tin bán " + ex.ToString());
            }
        }

        public static bool DangTinBanSanPham(TinBanDTO tb, SanPhamDTO sp)
        {
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@MaSanPham", sp.MaSanPham));
                arrParameter.Add(new SqlParameter("@TenSanPham", sp.TenSanPham));
                arrParameter.Add(new SqlParameter("@TinhTrang", sp.TinhTrang));
                arrParameter.Add(new SqlParameter("@XuatXu", sp.XuatXu));
                arrParameter.Add(new SqlParameter("@MoTaChiTiet", sp.MoTaChiTiet));
                arrParameter.Add(new SqlParameter("@LoaiSanPham", sp.LoaiSanPham));
                arrParameter.Add(new SqlParameter("@MaTinBan", tb.MaTinBan));
                arrParameter.Add(new SqlParameter("@MaNguoiBan", tb.MaNguoiBan));
                arrParameter.Add(new SqlParameter("@TieuDeTinBan", tb.TieuDe));
                arrParameter.Add(new SqlParameter("@AnhDaiDien", tb.AnhDaiDien));
                arrParameter.Add(new SqlParameter("@ThoiGianBan", tb.ThoiGianBan));
                arrParameter.Add(new SqlParameter("@GiaBan", tb.GiaBan));
                arrParameter.Add(new SqlParameter("@TinhTrangTinBan", tb.TinhTrangTinBan));
                arrParameter.Add(new SqlParameter("@HinhThucBan", tb.HinhThucBan));
                arrParameter.Add(new SqlParameter("@LoaiVanChuyen", tb.LoaiVanChuyen));

                DataTable dt = DataProvider.ExecuteStoreProc("DangTinBanSanPham", arrParameter);
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi thêm mới tin bán " + ex.ToString());
            }
            return false;
        }
        
        #endregion

        public static string GetMaNguoiBan(string matinban)
        {
            string manguoiban;
            try
            {
                IList<SqlParameter> arrParameter = new List<SqlParameter>();
                arrParameter.Add(new SqlParameter("@MaTinBan", matinban));
                DataTable dt = DataProvider.ExecuteStoreProc("getMaNguoiBanTheMaTinBan", arrParameter);
                manguoiban = dt.Rows[0][0].ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy mã người bán theo mả tin bán " + ex.ToString());
            }
            return manguoiban;
        }

    }
}
